#include "qdicethread.h"

QDiceThread::QDiceThread()
{

}

void QDiceThread::startDice()
{
    this->t_running = true;
    this->t_pause = false;
}

void QDiceThread::pauseDice()
{
    this->t_pause = true;
}

void QDiceThread::endDice()
{
    this->t_running = false;
}

int QDiceThread::computeV()
{
    qsrand(QDateTime::currentDateTime().toTime_t());
    return qrand() % 6;
}

void QDiceThread::run()
{
    qDebug()<<"dice thread start";

    this->t_running = true;
    this->t_pause = true;

    while(this->t_running && (!this->isInterruptionRequested())) {
        qDebug()<<"start deice...";
        if (!this->t_pause) {
            qDebug()<< "compute dice...";
            this->seq++;
            this->v = this->computeV();

            emit newDiceValue(this->seq, this->v);
            qDebug()<< "seq:"<<this->seq<<"v:"<<this->v;
        }
        msleep(500);
        qDebug()<<this->isInterruptionRequested();
    }
    qDebug()<<this->isInterruptionRequested();
    qDebug()<<"dice thread stop";
    quit();
}
